//
//  HYInputView.m
//  TextViewKey
//
//  Created by SMC-MAC on 16/6/6.
//  Copyright © 2016年 SMC-MAC. All rights reserved.
//

#import "HYInputView.h"
#import "UIPlaceHolderTextView.h"

@interface HYInputView () <UITextViewDelegate> {
    CGRect _screenBounds;
}

@property (weak, nonatomic) IBOutlet UIPlaceHolderTextView *textView;
- (IBAction)onCancelButtonClicked:(id)sender;

/**
 *  键盘的y坐标
 */
@property (assign, nonatomic) CGFloat keyboardPy;

@end

@implementation HYInputView

- (void)awakeFromNib {
    _screenBounds = [[UIScreen mainScreen] bounds];
    self.keyboardPy = _screenBounds.size.height;
    self.limitLen = LIMIT_LEN_DEFAULT;
    self.textView.delegate = self;
    self.textView.scrollEnabled = NO;
    self.textView.returnKeyType = UIReturnKeySend;
    self.textView.placeholder = @"请输入信息...";
    
    // 监听键盘改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    //
    [self _updateFrame:33];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

/**
 *  监听键盘的frame即将改变的时候调用
 *
 *  @param note 键盘通知事件
 */
- (void)keyboardWillChange:(NSNotification *)note{
    // 获得键盘的frame
    CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.keyboardPy = frame.origin.y;
//    UIViewAnimationOptions option = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    // 执行动画
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect tFrame = self.frame;
    CGFloat py = self.keyboardPy - tFrame.size.height;
    CGRect nFrame = CGRectMake(0, py, tFrame.size.width, tFrame.size.height);
    WS(ws);
    [UIView animateWithDuration:duration animations:^{
        ws.frame = nFrame;
    }];
}

/**
 *  更新输入框的Frame，高度为 45.0f，UITextView高33，字号大小14.0f，一行高度是17
 *  文本框33，上下间距为6，共12.0f
 *  @param tSize 文本高度
 */
- (void)_updateFrame:(NSInteger)textHeight {
    CGSize sz = _screenBounds.size;
    CGFloat tHeight = textHeight + 12.0f;
    self.frame = CGRectMake(0, self.keyboardPy - tHeight, sz.width, tHeight);
}

#pragma mark -- UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    NSInteger number = [textView.text length];
    if (number > self.limitLen) {
        textView.text = [textView.text substringToIndex:self.limitLen];
        number = self.limitLen;
    }
//    NSInteger left = self.limitLen - number;
//    NSLog(@"left number = %ld", left);
    CGSize size = [textView sizeThatFits:CGSizeMake(CGRectGetWidth(textView.frame), MAXFLOAT)];
    NSInteger tHeight = ceilf(size.height);
    
    [self _updateFrame:tHeight];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"text in range == %@", text);
    if ([text isEqualToString:@"\n"]) {
        if (self.onTextEditDone) {
            NSString *content = textView.text;
            self.onTextEditDone(content);
            textView.text = @"";
            [self _updateFrame:33];
        }
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}

/**
 *  取消按钮点击事件
 *
 *  @param sender 取消按钮
 */
- (IBAction)onCancelButtonClicked:(id)sender {
    [self dismissKeyboard];
}

/**
 *  隐藏键盘
 */
- (void)dismissKeyboard {
    if ([self.textView isFirstResponder]) {
        [self.textView resignFirstResponder];
    }
}

@end
